package com.mcxx.modules.report.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.github.pagehelper.util.StringUtil;
import com.mcxx.common.ReportTargetEnum;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.report.service.impl.FundPayReportCompareService;
import com.mcxx.modules.base.service.read.AreaReadService;
import com.mcxx.modules.filemanage.easypoi.util.ExportExcelService;
import com.mcxx.modules.filemanage.file.service.FileService;
import com.mcxx.modules.miniSalvation.fundpay.entity.FundPayEntity;
import com.mcxx.modules.miniSalvation.fundpay.param.FundPayListSearchParam;
import com.mcxx.modules.miniSalvation.fundpay.service.read.FundPayReadService;
import com.mcxx.modules.statistics.dao.read.StatisDataItemReadDao;
import com.mcxx.modules.statistics.entity.FundPayReportCompare;
import com.mcxx.modules.statistics.entity.StatisDataItemEntity;
import com.mcxx.modules.statistics.entity.StatisItemEntity;
import com.mcxx.modules.statistics.param.StatisParam;
import com.mcxx.modules.statistics.service.read.StatisticItemReadService;
import com.mcxx.modules.system.service.write.TaskExportWriteService;
import com.mcxx.modules.temporary.fundpay.entity.TmpFundPayEntity;
import com.mcxx.modules.temporary.fundpay.param.TmpFundPayParam;
import com.mcxx.modules.temporary.fundpay.service.read.TmpFundPayReadService;
import com.mcxx.result.BaseResult;
import com.mcxx.util.*;
import com.yinhai.ta404.component.org.orguser.area.entity.TaAreaPo;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import com.mcxx.redis.client.RedisClusterUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import static java.util.stream.Collectors.toList;


@Slf4j
@Service
@TaTransactional
@Component
@EnableScheduling
public class ReportService {
    @Autowired
    protected AreaUtil areaUtil;
    @Autowired
    private AreaReadService areaReadService;
    @Autowired
    private RedisClusterUtils redisClusterUtils;
    @Autowired
    private StatisticItemReadService statisticItemReadService;
    @Autowired
    private FundPayReadService fundPayReadService;
    @Autowired
    protected StatisDataItemReadDao statisDataItemReadDao;
    @Autowired
    private TmpFundPayReadService tmpFundPayReadService;
    @Autowired
    private FundPayReportCompareService fundPayReportCompareService;
    @Autowired
    private ExportExcelService exportExcelService;
    @Autowired
    private FileService fileService;
    @Autowired
    private TaskExportWriteService taskExportWriteService;

    public BaseResult<Boolean> generateReport(String strAreaCodes, String strMonths, String strItemSets) {
        BaseResult<Boolean> result = new BaseResult<Boolean>();
        if(CommonUtil.invalidArgs(strMonths) || CommonUtil.invalidArgs(strAreaCodes)
                || CommonUtil.invalidArgs(strItemSets)) {
            throw new BizException(FailureCode.ERR_10001);
        }


        if(redisClusterUtils.hasKey("report_generate"+strAreaCodes)){
            throw new BizException(FailureCode.ERR_100003);
        }
        redisClusterUtils.set("report_generate"+strAreaCodes, "1", 60 * 60);

        String[] areaCodes = strAreaCodes.split(",");
        String[] months = strMonths.split(",");
        String[] itemSets = strItemSets.split(",");
        Arrays.sort(months);
        for(String areaCode : areaCodes){
            if(areaCode.length() != 12)
                continue;

            TaAreaPo curTaAreaPo = areaReadService.getAreaByCode(areaCode);
            for(String month : months){
                if(month.length() != 6)
                    continue;

                for(String itemSet : itemSets){
                    generateItemReport(curTaAreaPo, month, itemSet);
                }
            }
        }

        redisClusterUtils.del("report_generate" + strAreaCodes);
        return result;
    }

    private void generateItemReport(TaAreaPo curTaAreaPo, String month, String itemSet){
        List<StatisItemEntity> items =  statisticItemReadService.queryItemsByItemSetId(itemSet);
        if(items.isEmpty())
            return;


        StatisDataService service = null;
        if(items.get(0).getBusinessType().isEmpty())
            return;

        int businessType = Integer.parseInt(items.get(0).getBusinessType());
        service = confirmTargetService(businessType,items.get(0).getExceptionalPovertyType());
        if(null == service)
            return;

        for(StatisItemEntity item : items){

            service.generateItemReport(curTaAreaPo, month, itemSet, item);

            service.generateAccumulativeReport(curTaAreaPo, month, itemSet, item);
        }
    }


    @Scheduled(cron = "0 05 0 26,27,28,29,30,31,1,2,3,4 * ?")
    public BaseResult<Boolean> autoGenerateReport() {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());

        int day = ca.get(Calendar.DAY_OF_MONTH);
        if(day <= 4)
            ca.add(Calendar.MONTH, -1);
        else if(day < 26)
            return new BaseResult<Boolean>();

        Date start = new Date();

        if(redisClusterUtils.hasKey("report_generate_all")){
            log.info("已经在别的服务中执行该统计任务");
            return null;
        }
        redisClusterUtils.set("report_generate_all", "1", 60 * 60);

        log.info("开始自动生成统计报表");
        String itemSetIds = "26241C2AE6BB4FC6AB51FCA168267CA8,ED6AAC37B1114E30A49BFD8E1FC0674C"
                + ",BFCC2BAC740D4791B021CF315E02DB7E,FBE432237585439DAE77204210FF5329,A63AD0FFE8944FA3BB67561DD8309FF5";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String month = sdf.format(ca.getTime());
        generateReport("430000000000", month, itemSetIds);
        redisClusterUtils.del("report_generate_all");
        log.info("报表统计月份：{},耗时{}秒", month, (new Date().getTime() - start.getTime())/1000);
        return new BaseResult<Boolean>();
    }



    @Scheduled(cron = "0 0 03 * * ?")
    public BaseResult<Boolean> autoGenerateDynamicReport() {
        Calendar ca = Calendar.getInstance();
        ca.setTime(new Date());

        Date start = new Date();

        if(redisClusterUtils.hasKey("report_generate_dynamic")){
            log.info("已经在别的服务中执行该统计任务");
            return null;
        }
        redisClusterUtils.set("report_generate_dynamic", "1", 60 * 60);

        log.info("开始自动生成家庭动态统计报表");
        String itemSetIds = "14F791FA1B76403EB3926490E729DCB6,14F791FA1B76403EB3926490E729DCB7,14F791FA1B76403EB3926490E729DCB8,14F791FA1B76403EB3926490E729DCB9";

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
        String month = sdf.format(ca.getTime());
        generateReport("430000000000", month, itemSetIds);
        redisClusterUtils.del("report_generate_dynamic");
        log.info("报表统计月份：{},耗时{}秒", month, (new Date().getTime() - start.getTime())/1000);
        return new BaseResult<Boolean>();
    }


    public BaseResult<Boolean> checkReportByFund(String areaCode, String month) {
        BaseResult<Boolean> result = new BaseResult<Boolean>(FailureCode.ERR_10040.getCode(), "");
        if(StringUtil.isEmpty(areaCode) || areaCode.length() != 12 || StringUtil.isEmpty(month) || month.length() != 6){
            result.setCode(FailureCode.ERR_10001.getCode());
            result.setMsg(FailureCode.ERR_10001.getMessage());
            return result;
        }

        TaAreaPo curAreaPro = areaReadService.getAreaByCode(areaCode);
        Integer level = curAreaPro.getAreaLevel();


        List<TaAreaPo> taAreaPos = areaReadService.getChildrenByLevel(areaUtil.getValidCode(areaCode, level),
                                                    Collections.singletonList(Constant.AreaLevel.COUNTY));
        List<String> areaCodes = taAreaPos.stream().map(TaAreaPo::getAreaCode).collect(Collectors.toList());


        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        Date curMonth = null;
        try {
            curMonth = sdf.parse(month + "01");
        } catch (ParseException e) {
            e.printStackTrace();
            result.setCode(FailureCode.ERR_10001.getCode());
            result.setMsg(FailureCode.ERR_10001.getMessage());
            return result;
        }

        Date preMonth = null;

        if(!month.endsWith("01")){
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(curMonth);
            calendar.add(Calendar.MONTH, -1);
            preMonth = calendar.getTime();
        }

        BaseResult<Boolean> temp = new BaseResult<Boolean>();
        for(String code : areaCodes){

            temp = checkTeKunReport(curMonth, preMonth, code, Constant.BusinessStandType.CITY_TEKUN,
                    "cityFundPay");
            if(!temp.getData()) {
                result.setMsg(result.getMsg() + temp.getMsg());
            }


            temp = checkTeKunReport(curMonth, preMonth, code, Constant.BusinessStandType.COUNTY_TEKUN,
                    "countyFundPay");
            if(!temp.getData()) {
                result.setMsg(result.getMsg() + temp.getMsg());
            }


            temp = checkDiBaoReport(curMonth, code, Constant.BusinessStandType.CITY_DIBAO,
                    "city_every_items");
            if(!temp.getData()) {
                result.setMsg(result.getMsg() + temp.getMsg());
            }


            temp = checkDiBaoReport(curMonth, code, Constant.BusinessStandType.COUNTY_DIBAO,
                    "county_every_items");
            if(!temp.getData()) {
                result.setMsg(result.getMsg() + temp.getMsg());
            }


            temp = checkLinShiReport(curMonth, preMonth, code);
            if(!temp.getData()) {
                result.setMsg(result.getMsg() + temp.getMsg());
            }
        }

        if(result.getMsg().isEmpty())
            result.setCode(FailureCode.SUC_0.getCode());

        return result;
    }

    private BaseResult<Boolean> checkLinShiReport(Date curMonth, Date preMonth, String areaCode){

        TmpFundPayParam param = new TmpFundPayParam();
        param.setPayDate(DateUtil.format(curMonth, "yyyyMM"));
        param.setAreaCode(areaUtil.getValidCode(areaCode, Constant.AreaLevel.COUNTY));
        List<TmpFundPayEntity> funds = tmpFundPayReadService.getFundPayList(param);

        Double total = 0d;
        for(TmpFundPayEntity fund : funds){
            total += fund.getPayTotal();
        }

        String msg = "";

        BaseResult<Boolean> result = new BaseResult<Boolean>();
        result.setData(false);
        StatisParam statisParam = new StatisParam();
        statisParam.setAreaCode(areaCode);
        statisParam.setMonth(DateUtil.format(curMonth, "yyyyMM"));
        statisParam.setBusinessType(Constant.BusinessCode.LINSHI);
        statisParam.setStatisItemCode("tempCumMoneys");
        List<StatisDataItemEntity> statiss = statisDataItemReadDao.queryStatisticItems(statisParam);
        if(statiss.size() > 1){
            result.setMsg("临时救助数据重复:month-" + statisParam.getMonth() + ";areaCode-" + areaCode + ";businessType-"
                        + Constant.BusinessCode.LINSHI + ";statisConfigCode-tempCumMoneys");
            return result;
        }
        else if(statiss.size() < 1) {
            if(total.equals(0d)){
                result.setData(true);
            }
            else{
                result.setMsg("临时救助数据遗漏:month-" + statisParam.getMonth() + ";areaCode-" + areaCode + ";businessType-"
                        + Constant.BusinessCode.LINSHI + ";statisConfigCode-tempCumMoneys");
            }
            return result;
        }

        StatisDataItemEntity statis = statiss.get(0);
        double sum = statis.getSum1() + statis.getSum2() + statis.getSum3() + statis.getSum4() + statis.getSum5() + statis.getSum6() + statis.getSum7();
        if(null != preMonth) {
            statisParam.setMonth(DateUtil.format(preMonth, "yyyyMM"));
            List<StatisDataItemEntity> preStatiss = statisDataItemReadDao.queryStatisticItems(statisParam);
            if(preStatiss.size() > 1){
                result.setMsg("临时救助数据重复:years-" + statisParam.getMonth() + ";areaCode-" + areaCode
                        + ";businessType-" + Constant.BusinessCode.LINSHI + ";statisConfigCode-tempCumMoneys");
                return result;
            }
            else if(preStatiss.size() == 1) {
                StatisDataItemEntity preStatis = preStatiss.get(0);
                sum -= preStatis.getSum1() + preStatis.getSum2() + preStatis.getSum3() + preStatis.getSum4()
                        + preStatis.getSum5() + preStatis.getSum6() + preStatis.getSum7();
            }
        }

        if(Math.abs(total - sum * 10000) > 1) {
            msg += "临时救助错:areaCode-"+areaCode+"名册金额-"+total+",与上月差-"+(sum * 10000);
        }

        if(!msg.isEmpty()) {
            result.setMsg(msg);
        }
        else {
            result.setData(true);
        }

        return result;
    }

    private BaseResult<Boolean> checkDiBaoReport(Date curMonth, String areaCode, String standType, String statisItemCode){

        FundPayListSearchParam param = new FundPayListSearchParam();
        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
        param.setPayDate(DateUtil.format(curMonth, "yyyy-MM-dd"));
        if(standType.equals(Constant.BusinessStandType.CITY_DIBAO))
            param.setBusinessType(Constant.StatisBusinessCode.CITY_DIBAO);
        else
            param.setBusinessType(Constant.StatisBusinessCode.COUNTY_DIBAO);
        param.setAreaCode(areaUtil.getValidCode(areaCode, Constant.AreaLevel.COUNTY));
        param.setStandType(standType);
        List<FundPayEntity> funds = fundPayReadService.getFundPayList(param);

        Double total = 0d;
        for(FundPayEntity fund : funds){
            total += fund.getPayTotal();
        }

        String msg = "";

        BaseResult<Boolean> result = new BaseResult<Boolean>();
        result.setData(false);

        StatisParam statisParam = new StatisParam();
        statisParam.setAreaCode(areaCode);
        statisParam.setMonth(DateUtil.format(curMonth, "yyyyMM"));
        statisParam.setBusinessType(Constant.BusinessCode.DIBAO);
        statisParam.setStatisItemCode(statisItemCode);
        List<StatisDataItemEntity> statiss = statisDataItemReadDao.queryStatisticItems(statisParam);
        if(statiss.size() > 1){
            result.setMsg("低保数据重复:years-"+statisParam.getMonth()+";areaCode-"+areaCode
                    +";businessType-"+statisParam.getBusinessType()+";statisItemCode-"+statisItemCode);
            return result;
        }
        else if(statiss.size() < 1) {
            if(total.equals(0d)){
                result.setData(true);
            }
            else{
                result.setMsg("低保数据遗漏:years-"+statisParam.getMonth()+";areaCode-"+areaCode
                        +";businessType-"+statisParam.getBusinessType()+";statisItemCode-"+statisItemCode);
            }
            return result;
        }

        if(Math.abs(total - statiss.get(0).getSum()) > 1) {
            msg += "低保金错:areaCode-"+areaCode+"名册金额-"+total+",报表金额-"+statiss.get(0).getSum();
        }

        if(!msg.isEmpty()) {
            result.setMsg(msg);
        }
        else {
            result.setData(true);
        }

        return result;
    }

    private BaseResult<Boolean> checkTeKunReport(Date curMonth, Date preMonth, String areaCode, String standType,
                                                 String statisItemCode){

        FundPayListSearchParam param = new FundPayListSearchParam();
        param.setFundType(Constant.FundType.CAPITAL_FUND_PAY);
        param.setPayDate(DateUtil.format(curMonth, "yyyy-MM-dd"));
        param.setBusinessType(Constant.StatisBusinessCode.TEKUN);
        param.setAreaCode(areaUtil.getValidCode(areaCode, Constant.AreaLevel.COUNTY));
        param.setStandType(standType);
        List<FundPayEntity> funds = fundPayReadService.getFundPayList(param);

        Double total = 0d;
        Double basic = 0d;
        Double nursing = 0d;
        for(FundPayEntity fund : funds){
            total += fund.getPayTotal();
            if(null != fund.getBasic())
                basic += fund.getBasic();
            if(null != fund.getNursing())
                nursing += fund.getNursing();
        }

        String msg = "";
        BaseResult<Boolean> result = new BaseResult<Boolean>();
        result.setData(false);


        StatisParam statisParam = new StatisParam();
        statisParam.setAreaCode(areaCode);
        statisParam.setMonth(DateUtil.format(curMonth, "yyyyMM"));
        statisParam.setBusinessType(Constant.BusinessCode.TEKUN);
        statisParam.setStatisItemCode(statisItemCode);
        List<StatisDataItemEntity> statiss = statisDataItemReadDao.queryStatisticItems(statisParam);
        if(statiss.size() > 1){
            result.setMsg("数据重复:month-" + statisParam.getMonth() + ";areaCode-"+areaCode
                     + ";businessType-130;statisItemCode-" + statisItemCode+";standType-" + standType);
            return result;
        }
        else if(statiss.size() < 1) {
            if(total.equals(0d)){
                result.setData(true);
            }
            else{
                result.setMsg("特困数据遗漏:month-" + statisParam.getMonth() + ";areaCode-" + areaCode
                        +";businessType-130;statisItemCode-" + statisItemCode + ";standType-" + standType);
            }
            return result;
        }

        Double disBasic = statiss.get(0).getSum();
        Double disNursing = statiss.get(0).getSum1() + statiss.get(0).getSum2() + statiss.get(0).getSum3();

        if(null != preMonth) {
            statisParam.setMonth(DateUtil.format(preMonth, "yyyyMM"));
            List<StatisDataItemEntity> preStatiss = statisDataItemReadDao.queryStatisticItems(statisParam);
            if(preStatiss.size() > 1){
                result.setMsg("数据重复:month-"+statisParam.getMonth()+";areaCode-"+areaCode
                        +";businessType-130;statisItemCode-"+statisItemCode+";standType-"+standType);
                return result;
            }
            else if(preStatiss.size() == 1) {
                disBasic -= preStatiss.get(0).getSum();
                disNursing -= preStatiss.get(0).getSum1() + preStatiss.get(0).getSum2() + preStatiss.get(0).getSum3();
            }
        }

        if(Math.abs(total - (disBasic + disNursing) * 10000) > 1){
            msg += "基本生活费和照料护理费总计错:areaCode-" + areaCode + "名册总计-" + total + ",与上月差-"
                    + ((disBasic + disNursing) * 10000);
        }

        if(Math.abs(basic - disBasic * 10000) > 1){
            msg += "基本生活费错:areaCode-" + areaCode + "名册金额-" + basic + ",与上月差-" + (disBasic * 10000);
        }

        if(Math.abs(nursing - disNursing * 10000) > 1){
            msg += "基本生活费错:areaCode-" + areaCode + "名册金额-" + nursing + ",与上月差-" + disNursing * 10000;
        }

        if(!msg.isEmpty()) {
            result.setMsg(msg);
        }
        else {
            result.setData(true);
        }
        return result;
    }


    public BaseResult<Boolean> generateReportSingleItem(String areaCodesParam, String monthsParam, String itemSetId, String itemParam) {
        BaseResult<Boolean> result = new BaseResult<Boolean>();
        if (CommonUtil.invalidArgs(monthsParam) || CommonUtil.invalidArgs(areaCodesParam)
                || CommonUtil.invalidArgs(itemSetId)) {
            throw new BizException(FailureCode.ERR_10001);
        }


        if (redisClusterUtils.hasKey("report_generateSingleItem" + areaCodesParam)) {
            throw new BizException(FailureCode.ERR_100003);
        }
        redisClusterUtils.set("report_generateSingleItem" + areaCodesParam, "1", 60 * 60);

        String[] areaCodes = areaCodesParam.split(",");
        String[] months = monthsParam.split(",");
        Arrays.sort(months);
        for (String areaCode : areaCodes) {
            if (areaCode.length() != 12)
                continue;

            TaAreaPo curTaAreaPo = areaReadService.getAreaByCode(areaCode);
            for (String month : months) {
                if (month.length() != 6)
                    continue;

                generateSingleItemReport(curTaAreaPo, month, itemSetId, itemParam);

            }
        }

        redisClusterUtils.del("report_generateSingleItem" + areaCodesParam);
        return result;
    }


    private void generateSingleItemReport(TaAreaPo curTaAreaPo, String month, String itemSetId, String itemParam) {

        List<StatisItemEntity> itemEntities =  statisticItemReadService.queryItemsByItemSetId(itemSetId);
        if(itemEntities.isEmpty())
            return;


        StatisDataService service = null;

        String [] items = itemParam.split(",");
        List<String> itemList = Arrays.asList(items);

        List<String> noRepeat = itemList.stream().distinct().collect(toList());
        for (String item : noRepeat) {
            for (StatisItemEntity itemEntity : itemEntities) {
                if (item.equals(itemEntity.getStatisItemCode())) {
                    int businessType = Integer.parseInt(itemEntity.getBusinessType());
                    service = confirmTargetService(businessType,itemEntity.getExceptionalPovertyType());

                    service.generateItemReport(curTaAreaPo, month, itemSetId, itemEntity);

                    service.generateAccumulativeReport(curTaAreaPo, month, itemSetId, itemEntity);
                }
            }
        }
    }


    private StatisDataService confirmTargetService(int businessType,String exceptionalPovertyType) {
        StatisDataService service = null;
        switch (businessType){
            case 110:
                service = SpringUtil.getBean("subCityCompService");
                break;
            case 120:
                service = SpringUtil.getBean("subCountryCompService");
                break;
            case 130:
                if(exceptionalPovertyType.equals("02"))
                    service = SpringUtil.getBean("povertyCityCompService");
                else
                    service = SpringUtil.getBean("povertyCountryCompService");
                break;
            case 500:
                service = SpringUtil.getBean("tempService");
                break;
            case 121:
                service = SpringUtil.getBean("subCountryRevealService");
                break;
            case 122:
                service = SpringUtil.getBean("subCityRevealService");
                break;
            case 133:
                service = SpringUtil.getBean("povertyCountryRevealService");
                break;
            case 134:
                service = SpringUtil.getBean("povertyCityRevealService");
                break;
            case 601:
                service = SpringUtil.getBean("lowerDynamicWatchService");
                break;
        }
        return service;
    }


    public Map<String, List<FundPayReportCompare>> fundPayWithReportCompare(String areaCode, String month) {
        Map<String, List<FundPayReportCompare>> resultMap = new HashMap<>();

        if(null == areaCode || areaCode.isEmpty())
            areaCode = AreaUtil.HUNANCODE;

        if(null == month || month.isEmpty()) {
            Calendar ca = Calendar.getInstance();
            ca.setTime(new Date());
            ca.add(Calendar.MONTH, 0);
            Date date = ca.getTime();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
            month = sdf.format(date);
        }


        FundPayListSearchParam fundParams = new FundPayListSearchParam();
        fundParams.setAreaCode(areaCode);
        fundParams.setPayDate(month);


        if(redisClusterUtils.hasKey("fundPay_report_compare" + areaCode)){
            throw new BizException(FailureCode.ERR_100003);
        }
        redisClusterUtils.set("fundPay_report_compare" + areaCode, "1", 60 * 60);



        List<FundPayReportCompare> list1 = fundPayReportCompareService.compareCountyLower(fundParams);
        if (!list1.isEmpty())
            resultMap.put(ReportTargetEnum.COUNTY_LOWER.getReportName() ,list1);

        fundParams.setAreaCode(areaCode);
        List<FundPayReportCompare> list2 = fundPayReportCompareService.compareCityLower(fundParams);
        if (!list2.isEmpty())
            resultMap.put(ReportTargetEnum.CITY_LOWER.getReportName() ,list2);

        fundParams.setAreaCode(areaCode);
        List<FundPayReportCompare> list3 = fundPayReportCompareService.compareCountyPoverty(fundParams);
        if (!list3.isEmpty())
            resultMap.put(ReportTargetEnum.COUNTY_POVERTY.getReportName() ,list3);

        fundParams.setAreaCode(areaCode);
        List<FundPayReportCompare> list4 = fundPayReportCompareService.compareCityPoverty(fundParams);
        if (!list4.isEmpty())
            resultMap.put(ReportTargetEnum.CITY_POVERTY.getReportName() ,list4);

        fundParams.setAreaCode(areaCode);
        List<FundPayReportCompare> list5 = fundPayReportCompareService.compareTemp(fundParams);
        if (!list5.isEmpty())
            resultMap.put(ReportTargetEnum.TEMP.getReportName() ,list5);

        redisClusterUtils.del("fundPay_report_compare" + areaCode);
        return resultMap;
    }


    public void generateExcel(XSSFWorkbook workbook, int sheetNum, String sheetTitle, String[] headers, List<FundPayReportCompare> result) {

        XSSFSheet sheet = workbook.createSheet();
        workbook.setSheetName(sheetNum, sheetTitle);


        sheet.setDefaultColumnWidth((short) 20);

        XSSFRow row = sheet.createRow(0);
        for (int i = 0; i < headers.length; i++) {
            XSSFCell cell = row.createCell((short) i);
            XSSFRichTextString text = new XSSFRichTextString(headers[i]);
            cell.setCellValue(text.toString());
        }

        if (result != null) {
            int index = 1;
            for (FundPayReportCompare obj : result) {
                row = sheet.createRow(index);

                row.createCell(0).setCellValue(obj.getAreaCode());
                row.createCell(1).setCellValue(obj.getAreaName());
                row.createCell(2).setCellValue(obj.getReportName());
                row.createCell(3).setCellValue(obj.getReportTarget());
                row.createCell(4).setCellValue(obj.getMsg());
                index++;
            }
        }
    }


    public String getReportName(String areaCode, String month) {


        TaAreaPo curTaAreaPo = null;
        StringBuilder name = new StringBuilder();
        List<String> areaCodes = areaUtil.getNodeTreePathFromRoot(areaCode);
        for (String code : areaCodes) {
            TaAreaPo taAreaPro = areaReadService.getAreaByCode(code);
            if (null != taAreaPro)
                name.append(taAreaPro.getAreaName());

            if (code.equals(areaCode)) {
                curTaAreaPo = taAreaPro;
            }
        }

        if (null == curTaAreaPo) {
            throw new BizException(FailureCode.ERR_100001);
        }
        return name.append(month.substring(0, 4)).append("年").append(month.substring(4)).append("月").toString();
    }
}
